package com.example.mybatisplus.controller;

import com.example.mybatisplus.generator.domain.Student;
import com.example.mybatisplus.generator.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName MyBatisPlusController
 * @Description
 * @Author huyingliang
 * @Date 2024/1/5 11:21
 */
@RestController
@RequestMapping("/my")
public class MyBatisPlusController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/test")
    public String test() {
        return "test";
    }

    @GetMapping("/getAll")
    public String getAll() {
        return studentService.list().toString();
    }

    @GetMapping("/insert/{password}")
    public String insert(@PathVariable("password") String password) throws NoSuchAlgorithmException {

        byte[] digest = MessageDigest.getInstance("SHA-256")
                .digest(password.getBytes());//[B@73a37f5e

        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(String.format("%02x", b));//c4cab95c72f3cb7cd9ac37eba48b592c66b0923c0dfad0a4f80a35e51c65953a
        }

        Student student = new Student();
        student.setStudentName("张三");
        student.setPhone("123456789");
        student.setAddress("beijing");
        student.setTeacheruuid(UUID.randomUUID().toString());
        student.setStudentPassword(sb.toString());

        return studentService.save(student) ? "success" : "fail";
    }

    /*经过测试 不论是单条数据的插入还是批量插入
    * 不论是 单条数据的保存还是批量保存
    *  MyMetaObjecthandler都是生效的*/
    @GetMapping("/insertBatch")
    public String insertBatch() {
        /*创建出10个Student对象*/
        ArrayList<Student> students = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Student student = new Student();
            student.setStudentName("张三" + i);
            student.setPhone("123456789");
            student.setAddress("beijing");
            student.setTeacheruuid(UUID.randomUUID().toString());
            student.setStudentPassword("123456");
            students.add(student);
        }
        studentService.saveBatch(students);
        return "success";
    }

    @GetMapping("/update")
    public String update() {
        List<Student> students = studentService.listByIds(List.of(1));
        students.forEach(student -> student.setStudentName("李四"));
        studentService.updateBatchById(students);
        return "success";
    }

    @GetMapping("/updateBatch")
    public String updateBatch() {
        List<Student> students = studentService.listByIds(List.of(8,9,10,11));
        students.forEach(student -> student.setStudentName("李四"));
        studentService.updateBatchById(students);
        return "success";
    }

}
